package com.nj9you.sdk.dlg;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.nj9you.sdk.utils.Utils;


/**
 * 网络任务提示框
 *
 * @author guangqing_lu
 */
public class ProgressMsgDialog extends Dialog implements
        View.OnClickListener {

    private final int SUCCESS_WAITING_TIME = 2000;// 对话框延迟关闭时间 单位毫秒

    private TextView mTitleText;// 标题
    private TextView mTipsText;// 提示
    private ImageView mTrueImage;// 成功图标
    private ImageView mWarningImage;// 警告图标
    private ProgressBar mLoadingProgress;// 等待图标
    private Button mConfirmButton;// 确定按钮

    private CharSequence styleTitle;// 标题样式
    private String mTitle;// 标题内容
    private String mTips;// 提示内容

    public ProgressMsgDialog(Context context) {
        super(context);
        setCanceledOnTouchOutside(false);
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams wAttrs = window.getAttributes();
        wAttrs.gravity = Gravity.CENTER | Gravity.TOP | Gravity.FILL_HORIZONTAL;
        window.setWindowAnimations(android.R.style.Animation_Dialog);
        window.setBackgroundDrawable(context.getResources().getDrawable(
                (android.R.color.transparent)));
        setCancelable(false);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(Utils.getLayoutId(getContext(), "jy_dialog_waiting_login"));
        initView();
    }

    private void initView() {
        mTitleText = (TextView) findViewById(Utils.getId(getContext(), "text_title"));
        mLoadingProgress = (ProgressBar) findViewById(Utils.getId(getContext(), "pb_loading"));
        mTrueImage = (ImageView) findViewById(Utils.getId(getContext(), "ic_true"));
        mWarningImage = (ImageView) findViewById(Utils.getId(getContext(), "ic_warning"));
        mTipsText = (TextView) findViewById(Utils.getId(getContext(), "text_content"));
        mConfirmButton = (Button) findViewById(Utils.getId(getContext(), "btn_failed"));

        mConfirmButton.setOnClickListener(this);
    }

    /**
     * 设置对话框内同
     *
     * @param title 标题
     * @param tips  提示
     */
    public void setProgressContent(String title, String tips) {
        mTitle = title;
        mTips = tips;
    }

    /**
     * 设置自定义标题
     *
     * @param style
     */
    public void setStyleTitle(CharSequence style) {
        this.styleTitle = style;
    }

    @Override
    public void show() {
        super.show();
        if (mTitle != null) {
            mTitleText.setText(mTitle);
        }
        if (styleTitle != null) {
            mTitleText.setText(styleTitle);
        }
        if (mTips != null) {
            mTipsText.setText(mTips);
        }
        mLoadingProgress.setVisibility(View.VISIBLE);
        mTrueImage.setVisibility(View.GONE);
        mWarningImage.setVisibility(View.GONE);
        mConfirmButton.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    /**
     * 响应成功调用
     *
     * @param tips 成功后的提示信息
     */
    public void setSuccessMessage(String tips, boolean delayClose) {
        mLoadingProgress.setVisibility(View.GONE);
        mTrueImage.setVisibility(View.VISIBLE);
        mWarningImage.setVisibility(View.GONE);
        mTipsText.setText(tips);
        mConfirmButton.setVisibility(View.GONE);

        if (delayClose) {
            new CountDownTimer(SUCCESS_WAITING_TIME, SUCCESS_WAITING_TIME) {

                @Override
                public void onTick(long millisUntilFinished) {
                }

                @Override
                public void onFinish() {
                    // 短暂时间后关闭对话框并回调
                    if (isShowing()) {
                        dismiss();
                    }
                }
            }.start();
        } else {
            dismiss();
        }
    }

    /**
     * 响应失败调用
     *
     * @param tips 失败后的提示信息
     */
    public void setFailureMessage(String tips) {
        mLoadingProgress.setVisibility(View.GONE);
        mTrueImage.setVisibility(View.GONE);
        mWarningImage.setVisibility(View.VISIBLE);
        mTipsText.setText(tips);
        mConfirmButton.setVisibility(View.VISIBLE);
    }

}
